from django.shortcuts import render, HttpResponse
from .models import Tag, news
from django.views.generic import View, ListView
from comment.models import Comment
from numpy import *


# Create your views here.

def new_list(request):
    # 取出所有博客文章
    new = news.objects.all()
    # 需要传递给模板（templates）的对象
    context = {'news': new}
    # render函数：载入模板，并返回context对象
    return render(request, 'news/index.html', context)


def news_detail(request, id):
    new = news.objects.get(id=id)
    # 浏览量 +1
    new.views += 1
    new.save(update_fields=['views'])
    new = news.objects.get(id=id)

    # 取出评论
    comments = Comment.objects.filter(new=id)

    # 需要传递给模板的对象
    context = {'new': new, 'comments': comments, }
    # 载入模板，并返回context对象
    return render(request, 'news/detail.html', context)

